package net.mihome.app.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;

import net.aydx84318.weather.bean.WeatherAsyncHttpResponseHandler;
import net.aydx84318.weather.bean.WeatherInfo;
import net.aydx84318.weather.bean.WeatherUtils;
import net.aydx84318.weather.ui.WeatherActivity;
import net.mihome.app.R;
import net.mihome.app.utils.PreferenceUtils;

import org.apache.http.Header;

@SuppressLint("NewApi") public class WeatherView extends LinearLayout {
	private ImageView im_weather_iconImageView;
	private TextView im_weather_titleTextView;
	private TextView im_weather_timeTextView;
	private TextView im_weather_countTextView;
	private TextView im_weather_tipTextView;
	private Context appContext;
	private MyViewLinster myViewLinster;
	public WeatherView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public WeatherView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	
	public WeatherView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		this.im_weather_countTextView=(TextView) findViewById(R.id.im_weather_count);
		this.im_weather_iconImageView=(ImageView) findViewById(R.id.im_weather_icon);
		this.im_weather_timeTextView=(TextView) findViewById(R.id.im_weather_time);
		this.im_weather_tipTextView=(TextView) findViewById(R.id.im_weather_tip);
		this.im_weather_titleTextView=(TextView) findViewById(R.id.im_weather_title);
		this.appContext= getContext().getApplicationContext();
	}

	/**
	 * 如果是天气预报，则启动获取天气预报的数据
	 * */
	public void initWeather(){
		if(myViewLinster!=null){
			myViewLinster.onBeforeLoad();
		}
		String city=PreferenceUtils.getPrefString(appContext,"weather_city", "杭州");
        im_weather_titleTextView.setText(city);
		WeatherUtils.getWeather(city,new WeatherAsyncHttpResponseHandler() {
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, WeatherInfo weatherInfo) {
				// TODO Auto-generated method stub
				String weatherString=weatherInfo.getResults().get(0).getWeather_data().get(0).getWeather()+" "+weatherInfo.getResults().get(0).getWeather_data().get(0).getTemperature();
				weatherString=weatherString+"   "+weatherInfo.getResults().get(0).getWeather_data().get(0).getWind();
				im_weather_timeTextView.setText(weatherString);
				ImageLoader.getInstance().displayImage(weatherInfo.getResults().get(0).getWeather_data().get(0).getDayPictureUrl(), im_weather_iconImageView);
				if(weatherInfo.getResults().size()>0&&weatherInfo.getResults().get(0).getIndex().size()>0){
                    im_weather_tipTextView.setText(weatherInfo.getResults().get(0).getIndex().get(0).getDes());
                }else{
                    im_weather_tipTextView.setText("");
                }
                if(weatherInfo.getResults().get(0).getPm25()!=null&&!weatherInfo.getResults().get(0).getPm25().equals("")){
                    im_weather_countTextView.setText("   PM2.5："+weatherInfo.getResults().get(0).getPm25());
                }else{
                    im_weather_countTextView.setText("");
                }
                setOnclickLinser(weatherInfo);
				if(myViewLinster!=null){
					myViewLinster.onLoadComplete();
				}
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, Throwable arg3) {
				// TODO Auto-generated method stub
				Toast.makeText(appContext,"网络异常，无法获取天气预报信息", Toast.LENGTH_SHORT).show();
				if(myViewLinster!=null){
					myViewLinster.onLoadComplete();
				}
                setOnclickLinserRefresh();
                setVisibility(View.GONE);
			}
		});
	}

	public MyViewLinster getMyViewLinster() {
		return myViewLinster;
	}

	public void setMyViewLinster(MyViewLinster myViewLinster) {
		this.myViewLinster = myViewLinster;
	}
	private void setOnclickLinser(final WeatherInfo weatherInfo){
        WeatherView.this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(WeatherView.this.getContext(),WeatherActivity.class);
                intent.putExtra("weatherInfo",weatherInfo);
                WeatherView.this.getContext().startActivity(intent);
            }
        });
    }
    private void setOnclickLinserRefresh(){
        WeatherView.this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                initWeather();
            }
        });
    }
}
